﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Xid.Controls.IsaExport
{
    class Files
    {
        public IEnumerable<String> getFirstFromDuplicates(List<String> files)
        {
            foreach (var group in getDuplicates(files))
        	{
                yield return group.First<String>();
            }
        }

        public List<String> removeDuplicates(List<String> files) {
            List<String> cfiles = files.Clone();
            foreach (var group in getDuplicates(cfiles))
            {
                // keep the first duplicate ... 
                foreach (var item in group.Skip(1))
                {
                    cfiles.Remove(item);
                }
            }
            return cfiles;
        }

        public IEnumerable<IGrouping<String, String>> getDuplicates(List<String> files) {
            return files.GroupBy(x => Path.GetFileName(x)).Where(g => g.Count() > 1);
        }

        public bool checkDuplicates(List<String> files)
        {
            return getDuplicates(files).Count() > 0;
        }

    }
}
